﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using Test2;
using PylonC.NETSupportLibrary;

namespace Test2
{
    public partial class Form0 : Form
    {
        private ImageProvider m_imageProvider = new ImageProvider(); /* Create one image provider. */
        private Bitmap m_bitmap = null; /* The bitmap is used for displaying the image. */

        public Form0()
        {
            InitializeComponent();


            /* 更新相机区域中可用设备的列表。Update the list of available devices in the upper left area. */
            UpdateDeviceList();
        }


        private void button1_Click(object sender, EventArgs ev)
        {
            /* Open the selected image provider. */
            if (deviceListView.SelectedItems.Count > 0)
            {
                /* Get the first selected item. */
                ListViewItem item = deviceListView.SelectedItems[0];
                /* Get the attached device data. */
                DeviceEnumerator.Device device = item.Tag as DeviceEnumerator.Device;


                /* Open the image provider using the index from the device data. */
                m_imageProvider.Open(device.Index);


            }
        }


        /* 更新相机区域中可用设备的列表。Updates the list of available devices in the upper left area. */
        private void UpdateDeviceList()
        {
            /* 请向设备枚举器询问设备列表。Ask the device enumerator for a list of devices. */
            List<DeviceEnumerator.Device> list = DeviceEnumerator.EnumerateDevices();

            ListView.ListViewItemCollection items = deviceListView.Items;

            /* 将每个新设备添加到列表中。Add each new device to the list. */
            foreach (DeviceEnumerator.Device device in list)
            {
                bool newitem = true;
                /* 对于每个枚举设备，检查它是否在列表视图中。For each enumerated device check whether it is in the list view. */
                foreach (ListViewItem item in items)
                {
                    /* 从列表视图项中检索设备数据。Retrieve the device data from the list view item. */
                    DeviceEnumerator.Device tag = item.Tag as DeviceEnumerator.Device;

                    if (tag.FullName == device.FullName)
                    {
                        /* Update the device index. The index is used for opening the camera. It may change when enumerating devices. */
                        //更新设备索引。索引用于打开照相机。它可能会在枚举设备时发生更改。
                        tag.Index = device.Index;
                        /* No new item needs to be added to the list view */
                        //不需要向列表视图添加新项
                        newitem = false;
                        break;
                    }
                }

                /* If the device is not in the list view yet the add it to the list view. 
                 如果设备不在列表视图中，请将其添加到列表视图中。*/
                if (newitem)
                {
                    ListViewItem item = new ListViewItem(device.Name);
                    if (device.Tooltip.Length > 0)
                    {
                        item.ToolTipText = device.Tooltip;
                    }
                    item.Tag = device;

                    /* Attach the device data. 
                     连接设备数据。*/
                    deviceListView.Items.Add(item);
                }
            }

            /* 删除已删除的旧设备. */
            foreach (ListViewItem item in items)
            {
                bool exists = false;

                /* 对于列表视图中的每个设备，检查设备枚举是否没有找到它。. */
                foreach (DeviceEnumerator.Device device in list)
                {
                    if (((DeviceEnumerator.Device)item.Tag).FullName == device.FullName)
                    {
                        exists = true;
                        break;
                    }
                }
                /* 如果枚举尚未找到设备，则从列表视图中移除。. */
                if (!exists)
                {
                    deviceListView.Items.Remove(item);
                }
            }
        }
    

        /*处理列表框中摄像机的选择。当前打开的设备被关闭，第一个选择的设备被打开。*/
        private void deviceListView_SelectedIndexChanged(object sender, EventArgs ev)
        {

            /*打开选定的图像提供程序 Open the selected image provider. */
            if (deviceListView.SelectedItems.Count > 0)
            {
                /* 获取第一个选定项目Get the first selected item. */
                ListViewItem item = deviceListView.SelectedItems[0];
                /* 获取附加的设备数据Get the attached device data. */
                DeviceEnumerator.Device device = item.Tag as DeviceEnumerator.Device;

                /*使用设备数据的索引打开图像提供程序 Open the image provider using the index from the device data. */
                m_imageProvider.Open(device.Index);

            }
        }

        /*如果已按下F5键，请更新设备列表。*/
        private void deviceListView_KeyDown(object sender, KeyEventArgs ev)
        {

            if (ev.KeyCode == Keys.F5)
            {
                ev.Handled = true;
                /* Update the list of available devices in the upper left area. */
                UpdateDeviceList();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OneShot();
        }

        /*开始抓取一个图像并处理异常。*/
        private void OneShot()
        {
            m_imageProvider.OneShot(); /* Starts the grabbing of one image. */
        }



    }
}
